<template>  
</template>  

<script>  
var main,receiver,filter;    
var _codeQueryTag = false;    
export default {  
	props: {
		scanType:{//扫描类型
			type:Number,//0不限，1库区、货架，2存货  3入库任务 4出库任务
			default:0
		}
	},
    data() {  
        return {  
            scanCode: ''  
        }  
    },  
    created: function (option) {  
        this.initScan()  
        this.startScan();    
    },    
    onHide:function(){    
        this.stopScan();    
    },  
    destroyed:function(){    
        /*页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果*/  
        this.stopScan();    
    },
	mounted(){
		//console.log(this.scanType)
	},
    methods: {  
        initScan() {  
            let _this = this;  
            main = plus.android.runtimeMainActivity();//获取activity  
            var IntentFilter = plus.android.importClass('android.content.IntentFilter');   
            filter = new IntentFilter();    
            filter.addAction("android.intent.ACTION_DECODE_DATA"); // 换你的广播动作  
            receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver',{  
            onReceive : function(context, intent) {  
                plus.android.importClass(intent);     
                let code = intent.getStringExtra("barcode_string");// 换你的广播标签  
                _this.queryCode(code);    
            }});   
        },    
        startScan(){    
            main.registerReceiver(receiver,filter);  
        },    
        stopScan(){  
            main.unregisterReceiver(receiver);    
        },    
        queryCode: function(code){  
            //防重复  
			// 暂时先连续重复2次处理着，还没找到解决方案
            if(_codeQueryTag)return false;    
            _codeQueryTag = true;    
            setTimeout(function(){    
                _codeQueryTag = false;    
            },150); 
			uni.vibrateLong()
			
			let secPage = this.globalData.toSecPage
			//其他单据
			if(this.scanType == 3 || this.scanType == 4){
				uni.$emit("scan_erp_order", code)
			}else{
				//扫描返回入库任务单号、拣货任务单号
				if(secPage == 'produce'||secPage == 'material'){
					uni.$emit("scan_erp_order", code)
					return
				}
				
				//扫描返回存货
				this.$Db.dealQrcode(code,this.scanType).then(res=>{
					if(secPage == 'kc'){
						uni.$emit("scan_kc", res)
					}
					
					if(res.type==4){
						if(secPage == 'inventory'){
							uni.$emit("scan_inventory", res)
						}
						if(secPage == ''){
							uni.$emit("getMarr", res.info)
						}
					}else{
						//扫描返回货架，库区
						if(secPage == 'shelves'){
							uni.$emit("scan_shelves", res)
						}
						//console.log(this.globalData.toSecPage)
						if(secPage == ''){
							uni.$emit("get_Shelves", res.info)
						}
					}
					//uni.$emit('scancode',res)
				}).catch(err=>{
					this.$Api.msg(err);
				})
			}
        }  
    }  
}  
</script>  